from django.db.models import Q
from django.http import JsonResponse
from friends.models import Friend
from user.models import UserModel
from django.core.cache import cache

# Create your views here.



# 获取所有列表
def getList(uid):
    Q1 = Q(requester=uid, status=1)
    Q2 = Q(recipient=uid, status=1)
    datas = []
    for f in Friend.objects.filter(Q1 | Q2):
        temp = {"img": "", "nickname": "", "status": "","uid":""}
        if f.requester.uid == uid:
            f = f.recipient
        else:
            f = f.requester
        temp['img'] = f.img.url
        temp['nickname'] = f.nickname
        temp['status'] = True if cache.get(f.uid + "_statu") else False
        temp['uid'] = f.uid
        datas.append(temp)
    return datas



# 返回好友列表
def friendList(request,uid):
    try:
        datas = getList(uid)
        return JsonResponse({"statu":True,"datas":datas})
    except Exception as e:
        print("friendList error  ",e)
        return JsonResponse({"statu":False})

# 两个用户啥关系
def realtive(reqeust,user1,user2):
    temp = UserModel.objects.filter(Q(uid=user1) | Q(uid=user2))
    if temp.count() == 2:
        Q1 = Q(requester=user1,recipient=user2,status=1)
        Q2 = Q(requester=user2,recipient=user1, status=1)
        Q3 = Q(requester=user1,recipient=user2,status=0)    # 已申请
        user = UserModel.objects.filter(uid=user2).first()
        datas = {
            "img":user.img.url,
            "nickname":user.nickname
        }
        if Friend.objects.filter(Q1 | Q2).exists():
            return JsonResponse({"statu":False,"reason":"已添加","datas":datas})
        elif Friend.objects.filter(Q3).exists():
            return JsonResponse({"statu":False,"reason":"已申请","datas":datas})
        else:
            return JsonResponse({"statu":True,"datas":datas}) # 未添加状态
    else:
        return JsonResponse({"statu":False,"reason":"用户不存在"})


# 查看好友请求
def getrequester(request,recipient):
    friends = Friend.objects.filter(recipient_id=recipient,status=0).select_related("requester")
    datas = []
    for f in friends:
        temp = {"id":f.id}
        f = f.requester
        temp['img'] = f.img.url
        temp['nickname'] = f.nickname
        datas.append(temp)

    return JsonResponse({"statu":True,"datas":datas})


# 发送好友请求
def add_friend(request):
    if request.method == "POST":
        requester = request.POST.get("requester")
        recipient = request.POST.get("recipient")
        login_id = cache.get("user_" + requester)
        # 如果是本人申请的话
        if login_id == requester:
                # 发送请求
                friends = Friend(
                    requester_id=requester,
                    recipient_id=recipient,
                    status=0
                )
                friends.save()  # 添加好友关系
                return JsonResponse({"statu": True})
        else:
            return JsonResponse({"statu":False,"reason":"不是该用户"})


# 同意好友请求
def re_friend(request, friendsId,t):
    try:
        friends = Friend.objects.filter(id=friendsId).first()
        result = {"statu":True,"type":""}
        if t == 1:
            friends.status = 1
            friends.save()
            result['type'] = "已同意"
        else:
            result['type'] = "已拒绝"
            friends.delete()    # 移出申请好友的数据库
        return JsonResponse(result)
    except Exception as e:
        print(e)
        return JsonResponse({"statu":False})

